// pages/shoppingCar/shoppingCar.js
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageURL: 'cloud://banmanshop.6261-banmanshop-1301943798/product1.png',
    show: false,
    array: [],
    radio: '',
    allMoneyprice: 0,
    allMoneyzc: 0,
    toshopPay: [],
    actions: [],
    address: '',
    changeListValue: '编辑',
    num: 1
  },
  // 全选
  onChange(event) {
    for (let i = 0; i < this.data.array.length; i++) {
      this.data.array[i].showbg = !this.data.array[i].showbg
    }
    if (this.data.radio === '1') {
      this.setData({
        radio: '',
        allMoneyprice: 0,
        allMoneyzc: 0,
        toshopPay: [],
        array: this.data.array
      })
      return
    } else {
      this.data.toshopPay = this.data.array
      this.setData({
        array: this.data.array
      })
    }
    this.setData({
      radio: event.detail
    })
    this.allMoney()
  },
  // 编辑完成按钮
  changeList() {
    if (this.data.changeListValue == '编辑') {
      this.setData({
        changeListValue: '完成'
      })
    } else {
      this.setData({
        changeListValue: '编辑'
      })
    }
  },
  // 选择单个列表
  bigBox(event) {
    let arr = []
    for (let i = 0; i < this.data.array.length; i++) {
      if (event.currentTarget.dataset.name.id === this.data.array[i].id) {
        this.data.array[i].showbg = !this.data.array[i].showbg
      }
      if (this.data.array[i].showbg) {
        arr.push(this.data.array[i])
      } else {
        for (let j = 0; j < this.data.toshopPay.length; j++) {
          if (this.data.array[i].id === this.data.toshopPay[j].id) {
            arr.splice(j, 1)
          }
        }
      }
    }
    this.setData({
      array: this.data.array,
      toshopPay: arr
    })
    this.allMoney()
  },
  // 改变 + 数量
  changeNum(item) {
    var that = this
    for (let i = 0; i < this.data.array.length; i++) {
      if (item.currentTarget.dataset.name.id == this.data.array[i].id) {
        this.data.array[i].num = item.detail
      }
    }
    wx.request({
      url: app.globalData.baseUrl + '/goods_shopcar/updateShopCarNumByUserCode',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        shopcarId: item.currentTarget.dataset.name.id,
        num: item.detail
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.allMoney()
      }
    })
  },
  // 总价
  allMoney() {
    this.setData({
      allMoneyprice: 0,
      allMoneyzc: 0,
    })
    var allMoneyprice = 0
    var allMoneyzc = 0
    for (let i = 0; i < this.data.toshopPay.length; i++) {
      allMoneyprice += this.data.toshopPay[i].price * this.data.toshopPay[i].num
      allMoneyzc += this.data.toshopPay[i].priceAfterDiscount * this.data.toshopPay[i].num
    }
    this.setData({
      allMoneyzc: allMoneyprice,
      allMoneyprice: allMoneyzc
    })
  },
  // 选择地址关闭
  onClose() {
    this.setData({
      show: false
    });
  },
  // 选择地址选项
  onSelect(event) {
    this.setData({
      address: event.currentTarget.dataset.name.area + event.currentTarget.dataset.name.detail,
      show: false
    })
  },
  // 选择地址打开
  onOpen() {
    this.setData({
      show: true
    })
  },
  // 移到收藏
  toCollection() {},
  // 删除
  todelList() {
    var that = this
    Dialog.confirm({
      title: '提示',
      message: '确定要删除吗？'
    }).then(() => {
      // on confirm
      let arr = []
      for (let i = 0; i < this.data.toshopPay.length; i++) {
        arr.push(this.data.toshopPay[i].id)
      }
      wx.request({
        url: app.globalData.baseUrl + '/goods_shopcar/delShopCarByUser',
        data: {
          xcxOpenId: app.globalData.xcxOpenId,
          shopcarId: arr
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.code == 0) {
            that.search()
          }
        }
      })
    }).catch(() => {
      // on cancel
    })
  },
  topayShop() {
    // console.log(555)
    var that = this
    let check = that.data.toshopPay
    if (check.length == 0) {
      return
    }
    wx.setStorage({
      data: that.data.toshopPay,
      key: 'toshopPay',
    })
    wx.setStorage({
      data: that.data.allMoneyzc,
      key: 'allMoneyzc',
    })
    wx.setStorage({
      data: that.data.allMoneyprice,
      key: 'allMoneyprice',
    })
    wx.navigateTo({
      url: '/pages/payShop/payShop'
    })
  },
  search() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/goods_shopcar/getShopCarByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        pageNum: 1,
        pageSize: 10
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        let arr = res.data.data.rows
        for (let i = 0; i < arr.length; i++) {
          // const element = array[i];
          if (arr[i].productSpecsDTO != null) {
            arr[i].productSpecsDTO.specsAttId = app.globalData.baseUrl + '/getFile?id=' + arr[i].productSpecsDTO.specsAttId
          }
        }
        that.setData({
          array: arr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    if (app.globalData.xcxOpenId == '') {
      wx.showToast({
        title: '请先登录，再查看购物车',
        icon: 'none'
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '../me/me',
        })
      }, 2000)
    }
    var that = this
    // 地址
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressListByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        pageNum: 1,
        pageSize: 10
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.setData({
          actions: res.data.data
        })
      }
    })
    this.search()
  }
})